/*
 * customer权限banner列表
 * @Author: zfx
 * @Date: 2018-06-19 17:53:05
 */
<style lang="less" rel="stylesheet/less" type="text/less" >
.v-customer-banner-index{
	.ivu-table-column-center{
		padding: 5px 0;
	}
	.ivu-table-cell{
		padding-left: 10px;
		padding-right: 10px;
	}
	.info-banner{
        display: flex;
        justify-content: center;
		white-space: nowrap;
		padding: 5px 0;
	}
	.info-thumb{
		width: 120px;
		height: 120px;
		cursor: pointer;
		display: flex;
		align-items: center;
		img{
			width: 100%;
		}
	}
	.deleted, .hot, .new, .sale {
		cursor: pointer;
	}
}
</style>
<template>
	<div class="v-customer-banner-index">

		<Table
			:columns="columns"
			:loading="loading"
			:data="banners"
			:height="tableHeight()"
			border
			class="personal-scrollbar"
			@on-sort-change="sortable"/>
	</div>
</template>

<script>
import { Tag, Button } from 'iview';
import showPictureMixin from 'src/views/mixins/showPictureMixin';
import { computedTableHeight } from 'src/views/mixins/computedTableHeight';

export default {
	name: 'BannerIndex',
	mixins: [showPictureMixin, computedTableHeight],
	props: {
		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
		banners: {
			type: Array,
			default() {
				return [];
			},
		},
		jumpType: {
			type: Array,
			default() {
				return [];
			},
		},
	},
	data() {
		return {
			columns: [
				{
					title: this.$t('banner.name'),
					key: 'name',
					align: 'center',
					render: (h, { row }) => <div> { row.name || '-' } </div>,
				},
				{
					title: this.$t('banner.thumb'),
					key: 'image',
					align: 'center',
					render: (h, { row }) => {
						const thumb_image = row.image ? <img src={ row.image } on-click={() => this.showPicture(row.image)} alt="无" /> : '无';
						return (
							<div class="info-banner">
								<div class="info-thumb">{ thumb_image }</div>
							</div>
						);
					},
				},
				{
					title: this.$t('banner.specified_type'),
					key: 'get_stores',
					width: 140,
					align: 'center',
					render: (h, { row }) => (
						<div class="info-banner">
							{
								row.get_stores.length === 0 ? <div>{ this.$t('banner.device_number') }</div>
									: <div>{ this.$t('banner.store') }</div>
							}
						</div>
					),
				},
				{
					title: this.$t('banner.tag'),
					key: 'get_device_accounts',
					width: 140,
					align: 'center',
					render: (h, { row }) => {
						let tags = this.$t('common.none');
						if (Array.isArray(row.get_device_accounts) && row.get_device_accounts.length > 0) {
							tags = row.get_device_accounts.map(tag => <Tag type="border">{ tag.account }</Tag>);
						} else {
							tags = row.get_stores.map(tag => <Tag type="border">{ tag.name }</Tag>);
						}
						return (<div>{ tags }</div>);
					},
				},
				{
					title: this.$t('banner.jump_type'),
					key: 'banner_type_id',
					width: 140,
					align: 'center',
					render: (h, { row }) => <div> { this.jumpType[row.banner_type_id - 1].name || '-' } </div>,
				},
				{
					title: this.$t('banner.priority'),
					key: 'priority',
					align: 'center',
					width: 90,
					sortable: 'custom',
					render: (h, { row }) => <div> { String(row.priority) || '-' } </div>,
				},
				{
					title: this.$t('common.created_at'),
					width: 120,
					key: 'created_at',
					align: 'center',
					render: (h, { row }) => {
						let date = <div>{ this.$t('common.none') }</div>;
						if (row.created_at) {
							const [day, time] = row.created_at.split(' ');
							date = (<div>
								<div>{day}</div>
								<div>{time}</div>
							</div>);
						}
						return date;
					},
				},
				{
					title: this.$t('common.start_time'),
					width: 120,
					key: 'started_at',
					align: 'center',
					render: (h, { row }) => {
						let date = <div>{ this.$t('common.none') }</div>;
						if (row.started_at) {
							const [day, time] = row.started_at.split(' ');
							date = (<div>
								<div>{day}</div>
								<div>{time}</div>
							</div>);
						}
						return date;
					},
				},
				{
					title: this.$t('common.end_time'),
					width: 120,
					key: 'ended_at',
					align: 'center',
					render: (h, { row }) => {
						let date = <div>{ this.$t('common.none') }</div>;
						if (row.ended_at) {
							const [day, time] = row.ended_at.split(' ');
							date = (<div>
								<div>{day}</div>
								<div>{time}</div>
							</div>);
						}
						return date;
					},
				},
				{
					title: this.$t('common.operation'),
					align: 'center',
					width: 140,
					render: (h, { row }) => (
						<div>
							<Button name="index__edit" size="small" style={{ marginRight: '5px' }} on-click={() => { this.$emit('edit', row); }} type="primary">
								{ this.$t('common.edit') }
							</Button>
							<Button name="index__delete" size="small" style={{ marginRight: '5px' }} on-click={() => { this.$emit('delete', row.id); }} type="error">
								{ this.$t('common.delete') }
							</Button>
						</div>),
				},
			],
		};
	},
	methods: {

		/**
         * 排序的回调
         * @param  {Object} param {对应列的属性对象,里面包含了column列属性，key值，order排序字段}
         * @return {undefined}
         */
		sortable(param) {
			const search_param = {
				sort_column: param.key,

				sort_by: param.order === 'normal' ? '' : param.order,
			};

			this.$emit('sort', search_param);
		},

	},
};
</script>
